<template>
  <div class="top-nav">
    <div style="width: 1200px;margin: 0 auto;">

      <el-menu class="subject-nav" default-active="1" mode="horizontal" background-color="#FF2832" text-color="#fff"
        active-text-color="#F6F621" @select="handleSelect">

        <el-menu-item index="8" disabled>
          <img src="../assets/DDlog.gif" class="imgs">
        </el-menu-item>

        <el-menu-item index="1">主页</el-menu-item>

        <el-menu-item index="2" v-if="role!==1" >管理</el-menu-item>

        <el-menu-item index="3">个人中心</el-menu-item>
        <el-menu-item index="4">
        <el-dropdown @command="handleCommand">
          <span class="el-dropdown-link">
            <i class="el-icon-arrow-down"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="a">我的地址</el-dropdown-item>
            <el-dropdown-item command="b">修改用户密码</el-dropdown-item>
            <el-dropdown-item command="c">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
        </el-menu-item>
        <el-menu-item index="9" class="my-blck" style="float:right" v-if="isLogin">
          <router-link to="/login">
            <el-button type="info" round size="mini" style="background: #FFF;color:#ff2832">登录</el-button>
          </router-link>
        </el-menu-item>

        <el-menu-item index="5" style="float:right" v-if="!isLogin"><img :src="imgurl"  class="avatar"/>{{accName}}</el-menu-item>

      </el-menu>
    </div>

  </div>
</template>

<script>
// import Cookie from 'js-cookie'
// import axios from 'axios'
/**
   * description  顶部导航栏
   */
import { getOneUser } from '@/api/user'
export default {
  name: 'topnav',
  // computed: {
  //   ...mapState(['user'])
  // },
  inject: ['reload'],
  data () {
    return {
      dialogInfo: false,
      dialogPassword: false,
      activeIndex: '1',
      imgurl: '',
      accName: '',
      isLogin: true,
      role: 1
    }
  },
  mounted () {
    this.isUserLogin()
    this.reload()
  },
  // mounted () {
  //   // window.location.reload()
  // },
  computed: {
  },
  methods: {
    //  判断是否登录显示头像
    isUserLogin () {
      const userId = JSON.parse(localStorage.getItem('userMsg')).id
      const userMsg = JSON.parse(localStorage.getItem('userMsg'))
      console.log(userId)
      if (userId) {
        getOneUser(userId).then(res => {
          if (res.data.data.portrait) {
            this.imgurl = 'http://localhost:4096/img/' + res.data.data.portrait
          } else {
            this.imgurl = 'http://localhost:4096/img/6a3f4961281aeac770f652b58b20d6e2.jpg'
          }
        })
        this.isLogin = false
        this.accName = userMsg.accName
        this.role = userMsg.role
        this.reload()
      } else {
        this.isLogin = true
      }
    },

    handleSelect (key, keyPath) {
      switch (key) {
        case '1':
          if (this.$route.path !== '/index') {
            this.$router.push({
              path: '/index'
            })
          }
          break
        case '2':
          if (this.$route.path !== '/manage') {
            this.$router.push({
              path: '/manage'
            })
          }
          break
        case '3':
          if (this.$route.path !== '/core') {
            this.$router.push({
              path: '/core'
            })
          }
          break
      }
    },
    handleCommand (command) {
      switch (command) {
        case 'a':
          if (this.$route.path !== '/address') {
            this.$router.push({
              path: '/address'
            })
          }
          break
        case 'b':
          if (this.$route.path !== '/changepass') {
            this.$router.push({
              path: '/changepass'
            })
          }
          break
        case 'c':
          this.logOut()
          break
      }
    },
    // 登出
    logOut () {
      localStorage.removeItem('userMsg')
      localStorage.removeItem('userToken')
      this.$router.push({
        path: '/login'
      })
    }
    // 修改头像用户名

  }
}
</script>

<style scoped>
.imgs{
  height: 60px;
}
  .el-menu.el-menu--horizontal {
    border-bottom: solid 0px #e6e6e6;
  }

  .el-menu-item.is-disabled {
    padding: 0;
    opacity: 1;
  }

  .title-nav {
    color: #72fbec;
    font-weight: 900;
    font-size: 20px;
    letter-spacing: 4px;
  }

  .top-nav {
    height: 60px;
    width: 100%;
    background: rgb(100, 100, 172);
  }

  .el-menu--horizontal>.el-menu-item {
    height: 60px;
    line-height: 60px;
  }

  .subject-nav {
    margin: 0 auto;
  }

  .el-menu-item.is-disabled {
    cursor: pointer;
  }

  .el-menu-item{
    font-size: 19px;
    font-family: 'Times New Roman', Times, serif;
  }
   .el-dropdown-link {
    cursor: pointer;
    color: #fff;
  }
  .el-icon-arrow-down {
    font-size: 12px;
  }
  .el-icon-arrow-down:before {
    color: white;
}
.avatar{
  border-radius: 20px;
  width: 40px;
  height: 40px;
}
</style>
